package com.example.riseofthedefender;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLSurfaceView.Renderer;

public class GlRenderer implements Renderer
{
	public static Context context;
	private Game game;

	public GlRenderer(Context context) 
	{
		GlRenderer.context = context;
	}

	public void onDrawFrame(GL10 gl) 
	{	
		game.update();
		
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		
		game.draw(gl);
		
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) 
	{
		if (height == 0) // Prevent A Divide By Zero By 
			height = 1; // Making Height Equal One
		
		GameVar.cWidth = width;
		GameVar.cHeight = height;
		GameVar.cWidthH = (int) (width * 0.5f);
		GameVar.cHeightH = (int) (height * 0.5f);
		
		game = new Game();
		game.init(gl);
		
		gl.glViewport(0, 0, width, height); // Reset The Current Viewport
		gl.glMatrixMode(GL10.GL_PROJECTION); // Select The Projection Matrix
		gl.glLoadIdentity(); // Reset The Projection Matrix

		gl.glOrthof(0, width, 0, height, -1, 1);
		gl.glMatrixMode(GL10.GL_MODELVIEW); // Select The Modelview Matrix
		gl.glLoadIdentity(); // Reset The Modelview Matrix
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) 
	{
		TextureManager.LoadEverything(gl, context);
		gl.glEnable(GL10.GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
		gl.glShadeModel(GL10.GL_SMOOTH); // Enable Smooth Shading
		gl.glClearColor(100.0f, 0.0f, 0.0f, 255f); // Black Background
		gl.glClearDepthf(1.0f); // Depth Buffer Setup
		gl.glDepthFunc(GL10.GL_LESS); // The Type Of Depth Testing To Do
		gl.glEnable(GL10.GL_DITHER);
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		gl.glEnable(GL10.GL_ALPHA_BITS);
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
		gl.glFrontFace(GL10.GL_CW);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, Square.vertexBuffer);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, Square.textureBuffer);
	}
}
